package com.jd.financeweb.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Dao层的父类
 * @author liqingbo
 */
public class BaseDao {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://123.57.17.193:3306/financeweb?serverTimezone=UTC";
    private static final String USER = "adminweb";
    private static final String PASSWORD = "123456";

    static {
        //加载驱动
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected Connection connection;
    protected PreparedStatement pstmt;
    protected ResultSet resultSet;

    /**
     * 获取连接对象
     * @return
     */
    public Connection getConnection() {
        try {
            connection=DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    /**
     * 增删改通用用法
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql, Object... params) {
        int result = 0;
        try {
            //获取连接
            this.getConnection();
            //获取PreparedStatement对象
            pstmt = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }

    /**
     * 查询通用方法
     * @param sql
     * @param params
     * @return
     */
    public ResultSet executeQuery(String sql, Object... params) {

        try {
            //获取链接
            this.getConnection();
            //获取PreparedStatament对象
            pstmt = connection.prepareStatement(sql);
            //参数赋值
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            resultSet = pstmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 关闭资源
     * @param connection
     * @param preparedStatement
     * @param resultSet
     */
    public void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
